<?php
require_once("../../class2.php");
require_once(HEADERF);
		$user_id = USERID;
		$user_name = USERNAME;
require_once("./includes/require_all.php");
CMS_activitySave("filestable.php");

ramka("open");

function rendPercentToColor($percent) {
	$pp=ceil($percent*2);
	$pt_temp = floor($percent*5.1);
	if($pt_temp > 255){
		$green = 255;
		$red = $pt_temp - $green;
	}
	else{
		$green = $pt_temp;
		$red = 0;
	}
	$pt = "rgb(".(255-$red).",".(0+$green).",".(0).")";
	return $pt;
}

	$wordsall=mysql_fetch_array(mysql_query("SELECT SUM( LENGTH(english) - LENGTH(REPLACE (english, ' ', ''))+1) FROM $text_table"));
	$wordsdone= mysql_fetch_array(mysql_query("SELECT SUM( LENGTH(english) - LENGTH(REPLACE (english, ' ', ''))+1) FROM $text_table  WHERE content!=english OR translator!='original' OR content REGEXP '[а-яА-Я ]{10,}'"));
	$charall= mysql_fetch_assoc(mysql_query("SELECT SUM(CHAR_LENGTH(english)) as en, SUM(CHAR_LENGTH(content)) as bg FROM $text_table WHERE content!=english OR translator!='original' OR content REGEXP '[а-яА-Я ]{10}'"));
	mysql_query("UPDATE admin_panel SET wordsall='$wordsall[0]', wordsdone='$wordsdone[0]', charall_translated='$charall[en]', charbg='$charall[bg]' WHERE game_prefix='$CMS_pref'");

$game_show_temp = mysql_fetch_array(mysql_query("select game_show from admin_panel where game_prefix='$CMS_pref'"));
$game_show = $game_show_temp[0];
if ($_GET['game']!=null and $game_show and $user_rights["SEE"]===1) {
	if(isset($_GET['style'])) {$style=$_GET['style'];} else $style="";

	//Counting all rows for translation, all rows with cyrillic chars in them and percent
	$q = "SELECT * FROM ".$text_table." WHERE content <> ''";
	$r = mysql_query($q);
	$all = mysql_num_rows($r); //Counter for all english rows for translation
	$all_cyr = 0; //Counter for all cyrillic rows that are already translated
	while($a = mysql_fetch_array($r)){
		$content = $a['content'];
		$text_id = $a['text_id'];
		if(preg_match("/([а-яА-Я]{1,}\s?[а-яА-Я]{1,})/u", $content) OR preg_match("/(_IGcoLLen){1}/", $text_id)){
			$all_cyr++;
		}
	}
	$percent = round(($all_cyr*100)/$all, 2);
	
	$translation_status = $database->fetch_array($database->query("select date, game_percent, increase from admin_panel where game_name = '$gamename'"));
	$perc = floatval(str_replace(",", ".", $translation_status['game_percent']));
	$p = $percent - $perc;
	$increase = floatval(str_replace(",", ".", $translation_status['increase']));
	$post_time = $translation_status['date'];
	$year = substr($post_time, 0, 4);
	$month = substr($post_time, 5, 2);
	$day = substr($post_time, 8, 2);
	$date = mktime(00, 00, 01, $month, $day, $year);
	$seconds = time() - $date;
	//$days = floor($seconds/84600);
	if($seconds < 84600){
		$increase += $p;
		$query = "UPDATE admin_panel SET rows_translated = $all_cyr, rows_all = $all, game_percent = '$percent', date = NOW(), increase = '$increase' WHERE game_name = '$gamename'";
		$database->query($query);
	}
	else{
		$query = "UPDATE admin_panel SET rows_translated = $all_cyr, rows_all = $all, game_percent = '$percent', date = NOW(), increase = '$p' WHERE game_name = '$gamename'";
		$database->query($query);
	}

	//Page content
	if ($percent==100) {$temp_color= "green"; $temp_color_class= "textGREEN";} elseif ($percent==0) {$temp_color= "red"; $temp_color_class= "textRED";} else {$temp_color= "yellow";  $temp_color_class= "textYELLOW";} 
	echo "<table width=100%><tr><TD ALIGN='left'> <a href='../' class='forumheader4' style='font-size:14px; margin-left:10px;'>НАЗАД</a></td>";
	if ($user_rights["ADD"]===1) {
		echo <<<HTML
					<TD ALIGN='right'> 
						<a href='listtranslations.php?game=$CMS_game' class='forumheader4' style='font-size:14px; margin-right:10px;'>Преводи</a>
						<a href='famehall.php?game=$CMS_game' class='forumheader4' style='font-size:14px; margin-right:10px;'>Заслуги</a>
					</td>
HTML;
	}
	echo "</tr></table>";
				$pp=ceil($percent*4);
				$pt= rendPercentToColor($percent);
				$rr=$all-$all_cyr;
				if ($rr==0) { $classcolor='RED'; }
				elseif ($rr===100) { $classcolor='GREEN'; }
				else $classcolor='YELLOW';
	echo <<<HTML
				<center class='forumheader4' style='margin-top:-20px; margin-left:-1px; padding-bottom:10px;'><h1 style='border-bottom:0px;' class='unselectable defcursor'>Файлове за превод <a title='Списък на файловете' href='filestable.php?game=$CMS_game&style=list'><img src='icons/filelist.png' HEIGHT='14px' WIDTH='12px' style='vertical-align:middle;'></a></h1></center>
				<center class="forumheader unselectable defcursor">
					<div style='width:400px; height:14px; border: 1px solid black; background-color:grey;' align='left'>
					<div style='width:{$pp}px; background-color:$pt; height:14px;'></div>
					</div>
					<div style='margin-top:-17px; font-size:12px; color: black; font-weight:bold;'>$percent %</div>
					</center>
					<div class="forumheader4" style='padding:2px;'></div>
					<center class="forumheader unselectable defcursor">
					<div class='textBLUE' style='font-size:14px;'><span style='font-weight:normal;'>Превод:</span> <span class='textGREEN'>$all_cyr</span> / <span class='textRED'>$all</span></div>
					<div class='textBLUE' style='font-size:14px;'><span style='font-weight:normal;'>Остават:</span> <span class='textYELLOW'>{$rr}</span></div>
				</center>
HTML;

	//Inserting all file names in an array
	$filelist= mysql_query("SELECT distinct file_name FROM $text_table GROUP BY file_name ORDER BY COUNT(file_name) DESC");
	$files= array();
	while($file = mysql_fetch_array($filelist)){
			$files[] = $file['file_name'];
	} unset ($filelist);
	$files_count= count($files);
	CMS_statistic2SAVE($files_count,$all,$all_cyr,$percent);

	CMS_lastApprovedEcho();

	$br = 0; //Counter for coordination proper start-end <tr> tags
	$page = get_page(); $per_page = $filesOnPage; $offset = $per_page*($page-1); $pages = ceil($files_count/$per_page);
	if($style=="list") { 
		$offset= 0;
		$val= mysql_fetch_array(mysql_query("SELECT files FROM cms_statisticTWO WHERE game='".mysql_real_escape_string($CMS_game)."' LIMIT 1"));
		$per_page= $val['files'];
	}
	for($i=$offset;$i<$offset+$per_page;$i++){
		if(isset($files[$i])){
			$q = "SELECT * FROM $text_table WHERE file_name = '".$files[$i]."' and content <> ''";
			$r = mysql_query($q);
			$all = mysql_num_rows($r); //Counter for all english rows for translation in every particular part
			$cyr = 0; //Counter for all cyrillic rows that are already translated in every particular part
			while($a = mysql_fetch_array($r)){
				$content = $a['content'];
				$text_id = $a['text_id'];
				if(preg_match("/([а-яА-Я]{1,}\s?[а-яА-Я]{1,})/u", $content) OR preg_match("/(_IGcoLLen){1}/", $text_id)){
					$cyr++;	
				}
			} $percent = round(($cyr*100)/$all, 2);
			
			if($cyr == $all){ $TDcolor = "green"; } elseif ($cyr>0) { $TDcolor = "yellow"; } else { $TDcolor = "red"; }
			switch ($extension = substr($files[$i], -3)) {
				case "txt": $file_icon= "TXT_1.png"; break;
				case "xml": $file_icon= "XML_1.png"; break;
				case "int": $file_icon= "INT_1.png"; break;
				case "ini": $file_icon= "INI_1.png"; break;
				case "csv": $file_icon= "CSV_1.png"; break;
				case "nls": $file_icon= "NLS_1.png"; break;
				case "nlu": $file_icon= "NLU_1.png"; break;
				default: $file_icon= "FILE_1.png"; break;
			}
			
			$aricon[]= $file_icon; $arTDcolor[]= $TDcolor; $arname[]= $files[$i]; $arpercent[]= $percent; $arcyr[]= $cyr; $arall[]= $all; 
		}
	}

	if ($style!="list") {
		function print_sections($style=null,$start,$end) {
			global $arname, $arTDcolor, $arpercent, $arcyr, $arall, $counted, $tablewidth, $CMS_game;

			if($end+1>$counted) $end=$counted-1;
			echo <<<HTML
						<table class="unselectable filestable-border" style='$style' CELLSPACING='0' CELLPADDING ='3' WIDTH=$tablewidth%>
						<tr class="forumheader3" align="middle">
						<td></td>
						<td></td>
						<td class="textWHITE">Прогрес</td>
						<td class="textWHITE">Преводи</td>
						</tr>
HTML;
			for($i=$start; $i<=$end; $i++) {
				if($a%2==0) $class="cmsRow3"; else $class="cmsRow4"; $a++;
				$classhover=$class."-".strtoupper($arTDcolor[$i]);
				$classtext="text".strtoupper($arTDcolor[$i]);
				$ar_t= $arall[$i]-$arcyr[$i];
				echo <<<HTML
							<tr class="$classhover unselectable" style="cursor:pointer;"
								onclick="javascript: window.location='viewtext.php?game=$CMS_game&name=$arname[$i]';">
							<td><img src="icons/file_icon.gif" style="vertical-align:middle"/></td>
							<td><span class='$classtext'><b>{$arname[$i]}</b></span></td>
							<td align="middle">{$arpercent[$i]}%</td>
							<td align="middle"><span class="textGREEN2">{$arcyr[$i]}</span>/<span class="textRED2">{$arall[$i]}</span> ({$ar_t})</td>
							</tr>
HTML;
			} echo "</table>";
			return ++$end;
		}
		function print_all_sections() {
			global $pertable, $filelist_tables;
			$next=0;
			for($num=1; $num<=$filelist_tables; $num++) {
				$next= print_sections("float:left;",$next,$next+$pertable);
			}
		}
		
		$counted= count($arname); $pertable=ceil($counted/$filelist_tables)-1; $tablewidth= 100/$filelist_tables;
		echo "<div style='width:100%;'>";
		print_all_sections();
		echo "<div style='clear:both;' class='forumheader'><center class='textBLACK'>--</center></div>";
		echo "</div>";
		
	} else {

		echo "<table class='unselectable' CELLSPACING='0' CELLPADDING ='10' WIDTH=100% style='border-top:1px solid lightblue; border-bottom:1px solid lightblue;'>";
		foreach ($arname as $i=> $file) {
			$all_files=count($arname); 
			if(isset($file[25]) AND $file[25]!="" AND $all_files>1) $file= "".substr($file,0,15)."[...]".substr($file,-4);
			if($br % $filerows == 0) echo "<tr class='forumheader3'>";
			$classtext="text".strtoupper($arTDcolor[$i]);
			$pp=ceil($arpercent[$i]*2);
			$pt= rendPercentToColor($arpercent[$i]);
			$pw= 600/$filerows; $pw2= $pw+4;
			$pp= ceil(($pw/100)*$arpercent[$i]);
				if ($arpercent[$i]==0) { $classcolor='RED'; }
				elseif ($arpercent[$i]<100) { $classcolor='YELLOW'; }
				else $classcolor='GREEN';
			
			if(!isset($_GET['page'])) $_GET['page']=1;
			
			if(isset($_GET['debug']) and $_GET['debug']==='listurls') {
				if(isset($tcnt)) {$tcnt++;} else $tcnt=1;
				echo "http://playonbg.info/translations/cms/viewtext.php?game=$CMS_game&name=$arname[$i]&sby=$_GET[page] <br>";
				if($tcnt%50==0) echo "<br>";
				}
				
			echo <<<HTML
						<td class='bluehover' title='Отвори файла: $arname[$i]'>
						<a href='viewtext.php?game=$CMS_game&name=$arname[$i]&sby=$_GET[page]'>
						<center>
						<div style='padding-top:4px; width:{$pw2}px;'>
						<img src='icons/pack1/$aricon[$i]' WIDTH='64' HEIGHT='64' alt='$arname[$i]'/>
						<br>
						<b class='text$classcolor'>$file</b>
						</div>
						<div style='width:{$pw}px; height:6px; border: 1px solid black; background-color:grey;' align='left'>
							<div style='width:{$pp}px; background-color:$pt; height:6px;'></div>
						</div>
						<div style='width:{$pw2}px;'><span class="textGREY">Процент:</span> <span class='text$classcolor'>$arpercent[$i]%</span></div>
						<div style='width:{$pw2}px;'><span class="textGREY">Превод:</span> <span class='text$classcolor'>$arcyr[$i]/$arall[$i]</span>
						</div></center></a>
						</td>
HTML;
			$firstpassed=false;
			if($br % $filerows == $filerows - 1) {echo "</tr>"; $firstpassed=true;}	
			$br++;
		} 
		if($br%$filerows!=0 && $firstpassed===true) 
			{
				while ($br%$filerows!=0) {
					echo "<td></td>";
					$br++;
				} echo "</tr>";
		}
		echo "</table>";
	}

		if($pages > 1 and $style!='list'){
		
			$q = "select * from cms_pagestatus where game_name = '".mysql_real_escape_string($CMS_game)."'";
			$r = mysql_query($q);
			if(mysql_num_rows($r) == 0){
				for($i=1;$i<=$pages;$i++){
					$q = "insert into cms_pagestatus (game_name, game_page) values ('".mysql_real_escape_string($CMS_game)."', $i)";
					mysql_query($q);
				}
			}
			if(!in_array("red", $arTDcolor) and !in_array("yellow", $arTDcolor)){
				mysql_query("update cms_pagestatus set page_status = 'green' where game_name = '".mysql_real_escape_string($CMS_game)."' and game_page = ".mysql_real_escape_string($page)."");
			}
			elseif(in_array("yellow", $arTDcolor)){
				mysql_query("update cms_pagestatus set page_status = 'yellow' where game_name = '".mysql_real_escape_string($CMS_game)."' and game_page = ".mysql_real_escape_string($page)."");
			}
			elseif(in_array("green", $arTDcolor) and in_array("red", $arTDcolor)){
				mysql_query("update cms_pagestatus set page_status = 'yellow' where game_name = '".mysql_real_escape_string($CMS_game)."' and game_page = ".mysql_real_escape_string($page)."");
			}
			elseif(!in_array("green", $arTDcolor) and !in_array("yellow", $arTDcolor)){
				mysql_query("update cms_pagestatus set page_status = 'red' where game_name = '".mysql_real_escape_string($CMS_game)."' and game_page = ".mysql_real_escape_string($page)."");
			}
			
			echo "<div class='forumheader'><center>";
			for($j=1;$j<=$pages;$j++){
				if($j == $page){
					echo "<span style='padding:3px;paddin-top:2px;'>$j</span>";
				}
				else{
					$q = "select page_status from cms_pagestatus where game_name = '".mysql_real_escape_string($CMS_game)."' and game_page = $j";
					$r = mysql_query($q);
					$a = mysql_fetch_array($r);
					
					if($a['page_status'] == "green"){
						echo "<span onclick=javascript:window.location='filestable.php?game=$CMS_game&page=$j' class='button textGREEN'>$j</span>";
					}
					elseif($a['page_status'] == "yellow"){
						echo "<span onclick=javascript:window.location='filestable.php?game=$CMS_game&page=$j' class='button textYELLOW'>$j</span>";
					}
					else{
						echo "<span onclick=javascript:window.location='filestable.php?game=$CMS_game&page=$j' class='button textRED'>$j</span>";
					}
				}
				if($j%30==0) echo "</center><center style='margin-top:5px;'>";
			}
			echo "</center></div>";
		}

} else echo "<div class='forumheader3 textRED'>Не е уточнена игра, която да бъде показана, играта е преведена, или нямате достъп до нея. Извиняваме се за неудобството.</div>";

ramka("close");

require_once(FOOTERF);
?>